﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using Vision.Common; 

namespace Vision.Utilities.CMS6UpdateTool
{
    public class AutoUpdateDatabaseProcessorConfigurationElement : AutoUpdateProcessorConfigurationElement<IUpdateProcessor>
    {
        #region Const
        const string PackageFolderPropertyName = "packageFolder";
        const string FilesPropertyName = "files";
        #endregion

        #region Ctors
        public AutoUpdateDatabaseProcessorConfigurationElement()
            : base()
        {
            Files = new OrderedNameValueConfigurationCollection();
        }

        public AutoUpdateDatabaseProcessorConfigurationElement(string name, Type type)
            : base(name, type)
        {
            Files = new OrderedNameValueConfigurationCollection();
        }
        #endregion

        #region Properties
        [ConfigurationProperty(PackageFolderPropertyName)]
        public string PackageFolder 
        {
            get { return (string)this[PackageFolderPropertyName]; }
            set { this[PackageFolderPropertyName] = value; }
        }

        [ConfigurationCollection(typeof(OrderedNameValueConfigurationCollection))]
        [ConfigurationProperty(FilesPropertyName, IsDefaultCollection = false)]
        public OrderedNameValueConfigurationCollection Files
        {
            get { return (OrderedNameValueConfigurationCollection)this[FilesPropertyName]; }
            set { this[FilesPropertyName] = value; }
        }
        #endregion

        #region Create Instance
        public override IUpdateProcessor CreateInstance()
        {
            var result = new DatabaseUpdateProcessor(PackageFolder, Files.Cast<OrderedNameValueConfigurationElement>().OrderBy(c => c.Order).Select(c => c.Value), Order);
            return result;
        }
        #endregion
    }
}